﻿/*----------------------------------------------------------------
 * 版权：http://XingFuStar.cnblogs.com
 * 
 * 文件名： XmlConfig
 * 文件功能描述： 简单的XML配置文件读写类
 * 
 * 作者：ZhuGe
 * 日期：2012年9月24日
 * 
 * 当前版本：V1.0.0
 * 
 * 修改日期：
 * 修改内容：
 *---------------------------------------------------------------*/


/*----------------------------------------------------------------
 * 相关的XML文件模版

<?xml version="1.0" encoding="utf-8"?>
<Configuration>
    <Section1>
        <Key1>Value</Key1>
        
        <KeyN>Value</KeyN>
    </Section1>
    
    <SectionN>
        <Key1>Value</Key1>
        
        <KeyN>Value</KeyN>
    </SectionN>
</Configuration>
 *----------------------------------------------------------------*/
using System;
using System.Xml;
using System.Diagnostics;

namespace ZhuGe.Common.Config
{
    public class XmlConfig
    {
        private string xmlPath;
        private bool isConfig;
        private XmlDocument xmlDocument;


        /// <summary>
        /// 构造函数：装载配置文件
        /// </summary>
        /// <param name="xmlPath">配置文件的路径</param>
        public XmlConfig(string xmlPath)
        {
            this.xmlDocument = new XmlDocument();
            this.XmlPath = xmlPath;
        }

        /// <summary>
        /// Xml配置文件的路径
        /// </summary>
        public string XmlPath
        {
            set
            {
                xmlPath = value;
                isConfig = OnXmlPathChange();
            }
        }

        /// <summary>
        /// 读取配置
        /// </summary>
        /// <param name="section">节点</param>
        /// <param name="key">键</param>
        /// <param name="value">返回的键值</param>
        /// <returns>键值</returns>
        /// <returns>是否读取成功</returns>
        public bool ReadConfig(string section, string key, ref string value)
        {
            bool isRead = false;
            try
            {
                if (isConfig)
                {
                    value = xmlDocument.SelectSingleNode("Configuration").SelectSingleNode(section).SelectSingleNode(key).InnerText;
                    isRead = true;
                }
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
            return isRead;
        }

        /// <summary>
        /// 写入配置
        /// </summary>
        /// <param name="section">节点</param>
        /// <param name="key">键</param>
        /// <param name="value">要写入的新键值</param>
        /// <returns>是否写入成功</returns>
        public bool WriteConfig(string section, string key, string value)
        {
            bool isWrited = false;
            try
            {
                if (isConfig)
                {
                    xmlDocument.SelectSingleNode("Configuration").SelectSingleNode(section).SelectSingleNode(key).InnerText = value;
                    xmlDocument.Save(xmlPath);
                    isWrited = true;
                }
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
            return isWrited;
        }

        #region 私有方法
        private bool OnXmlPathChange()
        {
            bool isLoaded = false;
            try
            {
                xmlDocument.Load(xmlPath);
                isLoaded = true;
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
            return isLoaded;
        }
        #endregion
    }
}
